package com.dynamic.datasource.transaction;

import org.springframework.util.StringUtils;

/**
 * @author 王先堂
 * @Description 事务上下文
 * @createTime 2022/03/28/ 15:36:00
 */
public class TransactionContext {
    /**
     * 事务id
     */
    private static final ThreadLocal<String> TRANSACTION_LOCAL = new ThreadLocal<>();

    /**
     * 获取当前线程事务id
     *
     * @return String
     */
    public static String get() {
        String id = TRANSACTION_LOCAL.get();
        if (!StringUtils.isEmpty(id)) {
            return id;
        }
        return null;
    }

    /**
     * 添加当前线程事务id
     *
     * @param id 事务id
     */
    public static void add(String id) {
        TRANSACTION_LOCAL.set(id);
    }

    /**
     * 移除当前线程事务id
     */
    public static void remove() {
        TRANSACTION_LOCAL.remove();
    }
}
